package cn.lisens.test.utils;

import cn.lisens.test.entity.ExportData;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import java.io.*;
import java.util.List;

public class JxlsExcelExportUtil {
    
    /**
     * 使用 JXLS2 导出 Excel
     * @param templatePath 模板文件路径
     * @param dataList 数据列表
     * @param exportTime 导出时间
     * @return Excel 字节数组
     * @throws IOException IO异常
     */
    public static byte[] exportToExcel(String templatePath, List<ExportData> dataList, String exportTime) throws IOException {
        try (InputStream templateInputStream = JxlsExcelExportUtil.class.getClassLoader()
                .getResourceAsStream(templatePath);
             ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            
            if (templateInputStream == null) {
                throw new FileNotFoundException("模板文件未找到: " + templatePath);
            }
            
            Context context = new Context();
            context.putVar("employeeList", dataList);
            context.putVar("exportTime", exportTime);
            
            JxlsHelper.getInstance().processTemplate(templateInputStream, outputStream, context);
            
            return outputStream.toByteArray();
        }
    }
}
